import Api from '@/services/api.js'
import mixin from '@/mixin/mixin.js'
const app = getApp()
export default {
  mixins: [mixin],
  components: {
  },
  data() {
    return {
      currProvice: {}, //当前省份
      currCity: {}, // 当前城市
      cityList: [], //城市列表
      scenicList: [],  //景点名称
      scenicArr: [],  //选中的城市
      proviceList: [], // 身份列表
    }
  },
  onLoad(options) {
    const { scenic } = options;
    if (scenic) this.scenicArr = JSON.parse(scenic);

    this.getScenicByCityId();
  },
  onShow() {
  },
  computed: {
    //状态栏高度
    statusBarHeight() {
      let { statusBarHeight } = app.globalData.systemInfo
      return statusBarHeight
    },
    //窗口宽度
    windowWidth() {
      let { windowWidth } = app.globalData.systemInfo
      return windowWidth;
    },
    location() {
      return this.$store.state.location;
    }
  },
  async mounted() {
    //默认展示第一个省的数据
    // this.currProvice = this.location.province;
    this.getCityList();
  },
  methods: {
    //根据省份id 获取城市列表
    async getCityList() {
      let params = {}
      if (this.currProvice.id) {
        params = { parentId: this.currProvice.id };
      }
      const { code, result } = await this.$request({
        url: Api.cityList,
        method: 'POST',
        data: params
      })
      if (code == 'suc') {
        if (this.currProvice.id) {
          this.cityList = result;
          this.currCity = result.find(i => i.id == this.location.city.id);
          this.getScenicByCityId();
        }else {
          this.proviceList = result;
          this.currProvice = result.find(i => i.id == this.location.province.id);
          this.getCityList();
        }
      }
    },
    // 根据城市id 获取城市
    async getScenicByCityId() {
      const { code, result } = await this.$request({
        url: Api.getScenicByCityId,
        data: {
          cityId: this.currCity?.id || ''
        }
      })
      if (code == 'suc') {
        result.map(i => {
          const scenic = this.scenicArr.find(v => v?.id == i.id);
          i.isChecked = scenic?.id == i.id;
          return i;
        })
        this.scenicList = result;
      } else if (code == 'list-empty') {
        this.scenicList = [];
      }
    },
    handleLeftItem(e) {
      const { item, arrField } = e.currentTarget.dataset
      // 选择省份
      if (arrField == 'filterProvice') {
        this.currProvice = item;
        this.getCityList();
      } else if (arrField == 'filterCity') {
        // 选择城市
        this.currCity = item;
        this.getScenicByCityId();
      } else {
        // 选择景点
        this.scenicList.forEach(i => {
          if (i.id == item.id) {
            i.isChecked = !i.isChecked;
            // 处理切换了城市之后保留选中态
            // 没有就添加 有就移出
            if (i.isChecked) {
              this.scenicArr.push(i);
            } else {
              const idx = this.scenicArr.findIndex(v => v?.id == i.id);
              this.scenicArr.splice(idx, 1);
            }
          }
        });
      }
      //todo  左侧记录item
    },
    // 底部按钮提交事件
    handleSubmit({ type }) {
      if (type == 'cancel') {
        // 重置
        this.scenicArr = [];
        this.scenicList.forEach(i => {
          i.isChecked = false
        })
      }else {
        if (this.scenicArr.length == 0) {
          return uni.showToast({title: '请先选择一个景点', icon: 'none'});
        }
        const eventChannel = this.getOpenerEventChannel();
        eventChannel.emit('selectScenicCallBack', this.scenicArr);
        uni.navigateBack();
      }
    }
  }
}